# encoding: utf-8
import sys, os
from app.modules.project.pomUtil import replaceProVersion, replaceJarVersion
from config import BaseConfig

reload(sys)
sys.setdefaultencoding("utf-8")


# 替换jar工程version
def replaceMavenJarVersion(codePath, newVersion):
    _v = None

    if not os.path.exists(codePath):
        print "the path %s is not exist" % codePath
        return _v
    _pomFile = '%s/pom.xml' % codePath

    if not os.path.exists(_pomFile):
        print "the pom file  %s is not exist" % _pomFile
        return _v

    removeBom(_pomFile)

    _v = replaceProVersion(_pomFile, newVersion, isAddProperties = True)
    print "the pom file  %s replace success . old version is %s " % (_pomFile, _v)
    return _v


# 替换maven war工程version
def replaceMavenWarVersion(trunkName, codePath, newVersion, jarObj):
    _v = None
    if not os.path.exists(codePath):
        print "the path %s is not exist" % codePath
        return _v
    _pomFile = '%s/pom.xml' % codePath

    # 替换主工程pom
    if not os.path.exists(_pomFile):
        print "the pom file  %s is not exist" % _pomFile
    else:
        removeBom(_pomFile)
        # 1.替换pom文件内工程version
        _v = replaceProVersion(_pomFile, newVersion, isAddProperties = True)
        print "the pom file %s version replace success . old version is %s " % (_pomFile, _v)
        # 2.替换pom文件内依赖工程version
        if len(jarObj) > 0:
            if trunkName in BaseConfig.NOT_CHECK_DEPENDENCIE:
                _obj = BaseConfig.NOT_CHECK_DEPENDENCIE[trunkName]
                for _key in _obj:
                    if _key in jarObj:
                        jarObj[_key]['key'] = _obj[_key]
                replaceJarVersion(_pomFile, jarObj, False, False)
            else:
                replaceJarVersion(_pomFile, jarObj, False, True)
            print "the pom file %s jar pro version replace success" % _pomFile
    return _v


def removeBom(file):
    BOM = b'\xef\xbb\xbf'
    existBom = lambda s: True if s == BOM else False

    f = open(file, 'rb')
    try:
        if existBom(f.read(3)):
            fbody = f.read()
            with open(file, 'wb') as f:
                f.write(fbody)
    finally:
        f.close()


# if __name__ == '__main__':
#     jarObj = {
#         'spi': {'groupId': 'com.winit', 'version': '1.1.1.spi'},
#         'winit-core': {'groupId': 'com.winit', 'version': '1.1.1.winit-core'},
#     }
    #     for name in ['cwm', 'ebay-adapter', 'em', 'erp-adapter', 'fna', 'fsp', 'fsp2', 'gfs', 'haitao', 'ims', 'lastmile',
    #                  'mms', 'mms2', 'mms3', 'nc-adapter', 'oms', 'oms2', 'openapi', 'operation-portal',
    #                  'pis', 'pms', 'pms2', 'report-center2', 'service-gate', 'sms', 'tms',
    #                  'tp', 'ums', 'ups', 'ups2', 'vms', 'wallet']:
    #         _jarObj = {
    #             'spi': {'groupId': 'com.winit', 'version': '1.1.1.spi', 'key': 'spi.version'},
    #             'winit-core': {'groupId': 'com.winit', 'version': '1.1.1.winit-core', 'key': 'winit.core.version'},
    #         }
    #         path = "/Users/wangjian/Desktop/test/%s" % name
    #         replaceMavenWarVersion(name, path, '1111111', _jarObj)
    #         pass
    #
    # name = 'oms2'
    # path = "/Users/wangjian/Desktop/test/%s" % name
    # replaceMavenWarVersion(name, path, '1111111', jarObj)


    # replaceMavenJarVersion(path, '1111111')

    # file = open('%s/pom.xml' % path,'r')
    # data = file.read()
    # print chardet.detect(data)
